<!-- 商品详情页 -->
<template>
  <div class="goods-details">
    <!-- 详情页的头部 -->
    <div class="details-header" ref="header">
      <!-- 箭头 -->
      <i
        :class="['iconfont', 'icon-jiantou3', iconBg]"
        onclick="history.back()"
      ></i>
      <!-- 商品详细tabs -->
      <div class="details-tabs" ref="tabs">
        <ul>
          <li
            v-for="(detailsTab, index) in detailsTabs"
            :key="index"
            :class="tabIndex == index ? 'active' : ''"
            @click="changeIndex(index)"
          >
            {{ detailsTab }}
          </li>
        </ul>
      </div>
      <i :class="['iconfont', 'icon-gerenguanzhu', iconBg]"></i>
      <i :class="['iconfont', 'icon-gengduo', iconBg]"></i>
    </div>
    <!-- 详情的内容 -->
    <div class="details-wrap">
      <div class="details_0 goods-info">
        <mt-swipe :show-indicators="false" @change="handleChange">
          <mt-swipe-item
            v-for="detailsItem in detailsSwiper"
            :key="detailsItem.id"
          >
            <img :src="detailsItem.pic" alt="" />
          </mt-swipe-item>
        </mt-swipe>
        <div class="custom-indicator">
          <strong>{{ current }}</strong
          >/
          <span>{{ detailsSwiper.length }}</span>
        </div>
        <!-- 商品详情的内容 -->
        <div class="details-content">
          <!-- 价格部分 -->
          <div class="details-price">
            <div class="shop_price">￥{{ detailsData.shop_price }}</div>
            <div class="market_price">￥{{ detailsData.market_price }}</div>
            <span class="sold_out">已售{{ detailsData.sales_volume }}件</span>
            <div class="share" @click="shareFun">
              <i class="iconfont icon-fenxiang"></i>
              <p>分享</p>
            </div>
          </div>
          <div class="goods_name_wrap">
            <div>
              <h3>
                <em>自营</em>
                <span>{{ detailsData.goods_name }}</span>
              </h3>
            </div>
          </div>
        </div>
        <!-- 配置模块内容 -->
        <div class="goods_module_wrap">
          <div class="activity_item">
            <div class="item-left">
              <h3>已选</h3>
            </div>
            <div class="item-center">
              <div class="tips">{{ buynum }}</div>
            </div>
            <div class="item-right" @click="openSelectPopup">
              <i class="iconfont icon-gengduo1"></i>
            </div>
          </div>
          <div class="activity_item">
            <div class="item-left">
              <h3>送至</h3>
            </div>
            <div class="item-center">
              <div class="tips">{{ province }}-{{ city }}-{{ area }}</div>
            </div>
            <div class="item-right" @click="openCitySelectPopup">
              <i class="iconfont icon-gengduo1"></i>
            </div>
          </div>
          <div class="activity_item">
            <div class="item-left">
              <h3>运费</h3>
            </div>
            <div class="item-center">
              <div class="tips">包邮</div>
            </div>
            <div class="item-right"></div>
          </div>
          <div class="activity_item">
            <div class="item-left">
              <h3>服务</h3>
            </div>
            <div class="item-center">
              <div class="tips">支持7天无理由退货1</div>
            </div>
            <div class="item-right">
              <i class="iconfont icon-gengduo1"></i>
            </div>
          </div>
        </div>
      </div>
      <div class="details_1 goods-info">评论</div>
      <div class="details_2 goods-info">详情</div>
      <div class="details_3 goods-info">推荐</div>
    </div>
    <!-- 海报的内容 -->
    <create-poster
      v-if="showposter"
      :detailsData="detailsData"
      @closePoster="closeFun"
    ></create-poster>
    <!-- 已经选择的弹出框 -->
    <div class="select-buy">
      <mt-popup v-model="popupVisible" position="bottom">
        <!-- 商品详情的内容 -->
        <div class="goods-wrap">
          <div class="goods-img">
            <img :src="detailsData.goods_thumb" alt="" />
          </div>
          <div class="goods-content">
            <div class="title">
              {{ detailsData.goods_name }}
            </div>
            <!-- 流通价格 -->
            <div class="currency-price">
              <span>
                <em>¥</em>
                <!-- 整数部分 -->
                <span>{{ detailsData.shop_price | setInt }}</span>
                <!-- 小数部分 -->
                <em>{{ detailsData.shop_price | setFloat }}</em>
              </span>
            </div>
          </div>
          <!-- 关闭字体图标 -->
          <i class="iconfont icon-chahao" @click="closeSelectPopup"></i>
        </div>
        <!-- 数量的内容 -->
        <div class="num-wrap">
          <h3>数量</h3>
          <div>
            <button class="minus">-</button>
            <input type="text" class="numtext" v-model="buynum" />
            <button class="add">+</button>
          </div>
        </div>
        <!-- 加入购物车和立即购买按钮 -->
        <div class="oper-wrap">
          <button class="addCart">加入购物车</button>
          <button class="buy">加入购物车</button>
        </div>
      </mt-popup>
    </div>
    <!-- 城市选择的弹出框 -->
    <div class="city-select">
      <mt-popup v-model="popupCityVisible" position="bottom">
        <h3>配送至</h3>
        <!-- 关闭字体图标 -->
        <i class="iconfont icon-chahao" @click="closeCitySelectPopup"></i>
        <mt-picker :slots="slots" @change="onValuesChange"></mt-picker>
      </mt-popup>
    </div>
    <div class="footer">
      <div class="shop">
        <i class="iconfont icon-dianpu"></i>
        <span>店铺</span>
      </div>
      <div class="kefu">
        <i class="iconfont icon-kefu2"></i>
        <span>客服</span>
      </div>
      <div class="cart" @click="toCartPage">
        <i class="iconfont icon-gouwuchekong"></i>
        <span>购物车</span>
        <em>10</em>
      </div>
      <button class="addCart">加入购物车</button>
      <button class="buy">立即购买</button>
    </div>
    <div style="width: 100%; height: 2000px"></div>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import CreatePoster from "./CreatePoster.vue";
import provinces from "@/mock/city.json";
let provincesData = provinces.provinces;
var tempProvinceArr = [];
provincesData.province.forEach(function (item) {
  tempProvinceArr.push(item.ssqname);
});
console.log(tempProvinceArr);
export default {
  //import引入的组件需要注入到对象中才能使用
  components: { CreatePoster },
  data() {
    //这里存放数据
    return {
      detailsTabs: ["商品", "评论", "详情", "推荐"],
      iconBg: "icon-bg",
      detailsSwiper: [
        {
          id: 1,
          pic: "https://x.dscmall.cn/storage/images/201703/goods_img/0_P_1489108266997.jpg",
        },
        {
          id: 2,
          pic: "https://x.dscmall.cn/storage/images/201703/goods_img/_P_1489108271727.jpg",
        },
        {
          id: 3,
          pic: "https://x.dscmall.cn/storage/images/201703/goods_img/_P_1489108271043.jpg",
        },
        {
          id: 4,
          pic: "https://x.dscmall.cn/storage/images/201703/goods_img/_P_1489108272459.jpg",
        },
      ],
      current: 0,
      tabIndex: 0,
      detailsData: {
        goods_name:
          "华帝【年度旗舰】Huawei/华为 Mate 9 6+128GB 4G智能手机限量抢 麒麟960芯片 徕卡双镜头",
        shop_price: "2333.00",
        market_price: "2799.00",
        sales_volume: 4,
        goods_thumb:
          "https://x.dscmall.cn/storage/images/201703/thumb_img/0_thumb_G_1489108266095.jpg",
      },
      buynum: 1,
      showposter: false,
      popupVisible: false,
      popupCityVisible: false,
      slots: [
        {
          flex: 1,
          values: tempProvinceArr,
          className: "slot1",
          textAlign: "left",
        },
        {
          divider: true,
          content: "-",
          className: "slot2",
        },
        {
          flex: 1,
          values: ["市辖区", "市辖县"],
          className: "slot3",
          textAlign: "center",
        },
        {
          divider: true,
          content: "-",
          className: "slot4",
        },
        {
          flex: 1,
          values: ["请选择"],
          className: "slot5",
          textAlign: "left",
        },
      ],
      province: "",
      city: "",
      area: "",
    };
  },
  //计算属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    scrollHeaderFun() {
      // 头部样式的更改
      if (document.documentElement.scrollTop >= 50) {
        this.$refs.header.className = "details-header details-header-scroll";
        this.$refs.tabs.className = "details-tabs details-tabs-scroll";
        this.iconBg = "";
      } else {
        this.$refs.header.className = "details-header";
        this.$refs.tabs.className = "details-tabs";
        this.iconBg = "icon-bg";
      }
      //   头部滚动时tabs的active的切换
      //   获取所有楼层的信息
      var detailsAllEle = document.querySelectorAll(".goods-info");
      // 获取头部的高度--减去高度
      var headerHeight = this.$refs.header.offsetHeight;

      for (var i = 0; i < detailsAllEle.length; i++) {
        if (window.scrollY + headerHeight >= detailsAllEle[i].offsetTop) {
          this.tabIndex = i;
        }
      }
    },
    handleChange(index) {
      this.current = index;
    },
    // 点击使元素进行跳动
    changeIndex(index) {
      this.tabIndex = index;
      // 元素距离上方的偏移量
      let detailsEle = document.querySelector(".details_" + index);
      //   求出被卷进去的距离
      var backDis =
        detailsEle.offsetTop -
        window.pageYOffset -
        this.$refs.header.offsetHeight;
      // 滚动的距离
      window.scrollBy({
        top: backDis,
        behavior: "smooth",
      });
    },
    shareFun() {
      this.showposter = true;
    },
    closeFun(data) {
      this.showposter = data;
    },
    openSelectPopup() {
      this.popupVisible = true;
    },
    closeSelectPopup() {
      this.popupVisible = false;
    },
    openCitySelectPopup() {
      this.popupCityVisible = true;
    },
    closeCitySelectPopup() {
      this.popupCityVisible = false;
    },
    // 城市选择picker
    onValuesChange(picker, values) {
      console.log(picker);
      console.log(values);
      let proDatas = provincesData.province;
      // 设置市的数据
      var city = [];
      for (let i = 0; i < proDatas.length; i++) {
        if (values[0] == proDatas[i].ssqname) {
          for (let j = 0; j < proDatas[i].cities.city.length; j++) {
            city.push(proDatas[i].cities.city[j].ssqname);
          }
        }
      }
      picker.setSlotValues(1, city);
      // 设置县的数据
      let area = [];
      for (let i = 0; i < proDatas.length; i++) {
        if (values[0] == proDatas[i].ssqname) {
          for (let j = 0; j < proDatas[i].cities.city.length; j++) {
            if (values[1] == proDatas[i].cities.city[j].ssqname) {
              for (
                let k = 0;
                k < proDatas[i].cities.city[j].areas.area.length;
                k++
              ) {
                area.push(proDatas[i].cities.city[j].areas.area[k].ssqname);
              }
            }
          }
        }
      }
      picker.setSlotValues(2, area);
      // 设置所有联动的数据
      // ['河北','石家庄','石家庄的县']
      this.province = values[0];
      this.city = values[1];
      this.area = values[2];
    },
    toCartPage() {
      this.$router.push("/cartpage");
    },
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    document.addEventListener("scroll", this.scrollHeaderFun);
  },
  beforeDestroy() {
    document.removeEventListener("scroll", this.scrollHeaderFun);
  },
};
</script>
<style lang="less">
.goods-details {
  .details-header {
    transition: all 1s;
    width: calc(100% - 2rem);
    height: 5rem;
    padding: 0 1rem;
    display: flex;
    justify-content: center;
    align-items: center;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 999;
    background-color: transparent;
    color: #fff;
    i {
      width: 2.5rem;
      height: 2.5rem;
      text-align: center;
      line-height: 2.5rem;
      margin-right: 1rem;
    }
    .icon-bg {
      background-color: rgba(41, 47, 54, 0.4);
      border-radius: 50%;
    }
    // tabs内容
    .details-tabs {
      width: 70%;
      opacity: 0;
      transition: all 0.5s;
      ul {
        display: flex;
        li {
          flex: 1;
          font-size: 1.5rem;
          text-align: center;
          color: #666;
          position: relative;
        }
        .active {
          font-weight: 700;
          color: #000;
          &::after {
            content: "";
            position: absolute;
            left: 30%;
            bottom: -0.5rem;
            width: 2.6rem;
            height: 0.3rem;
            background: linear-gradient(
              90deg,
              #f91f28,
              rgba(255, 79, 46, 0.35)
            );
          }
        }
      }
    }
    .details-tabs-scroll {
      opacity: 1;
    }
  }
  .details-header-scroll {
    color: #666;
    background-color: rgba(255, 255, 255, 0.9);
  }
  //   商品所有内容的包含
  .details-wrap {
    .details_0 {
      position: relative;
      .mint-swipe {
        width: 100%;
        height: 37.5rem;
        img {
          width: 100%;
          height: 37.5rem;
        }
      }
      .custom-indicator {
        position: absolute;
        text-align: center;
        right: 0;
        top: 30rem;
        width: 5rem;
        height: 2.5rem;
        line-height: 2.5rem;
        border-radius: 1.5rem 0 0 1.5rem;
        background-color: #ccc;
        font-size: 1.5rem;
        color: #fff;
      }
      .details-content {
        width: calc(100% - 2rem);
        padding: 1rem;
        background-color: #fff;
        .details-price {
          height: 4rem;
          display: flex;
          justify-content: space-between;
          align-items: center;
          .shop_price {
            font-size: 2.4rem;
            color: rgb(242, 14, 40);
            font-weight: 700;
          }
          .market_price {
            font-size: 1.4rem;
            color: #ccc;
            margin-left: -5rem;
            text-decoration: line-through;
          }
          .sold_out {
            color: #555;
            font-size: 1.4rem;
            margin-left: -5rem;
          }
        }
        .goods_name_wrap {
          width: calc(100% - 2rem);
          padding: 1rem;
          height: 6.75rem;
          background-color: #fff;
          h3 {
            font-size: 1.5rem;
            line-height: 2rem;
            em {
              background: linear-gradient(90deg, #f91f28, #ff4e2e);
              font-size: 1.2rem;
              padding: 0.2rem 0.4rem;
              width: 2rem;
              color: #fff;
              border-radius: 1rem;
              margin-right: 1rem;
            }
          }
        }
      }
      .goods_module_wrap {
        width: calc(100% - 2rem);
        padding: 1.5rem 1rem;
        border-radius: 1rem;
        background-color: #fff;
        margin: 1rem 0;
        .activity_item {
          padding: 1rem 0;
          display: flex;
          justify-content: space-between;
          h3 {
            font-size: 1.5rem;
          }
          .item-center {
            width: 80%;
            font-size: 1.3rem;
          }
        }
      }
    }
    .details_1,
    .details_2,
    .details_3 {
      width: 100%;
      height: 60rem;
      box-sizing: border-box;
      border: 1px solid blue;
    }
  }
  // 选择弹出框的样式
  .select-buy {
    .mint-popup {
      width: 100%;
      height: 27.7rem;
      background-color: #fff;
      border-radius: 1.5rem 1.5rem 0 0;
      .goods-wrap {
        width: calc(100% - 2rem);
        height: 12.8rem;
        padding: 1rem;
        display: flex;
        img {
          width: 9.2rem;
          height: 9.2rem;
        }
        .title {
          width: 90%;
          margin: 5%;
          height: 4rem;
          line-height: 2rem;
          font-size: 1.5rem;
          // 多行文本省略号
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
          overflow: hidden;
        }
        .currency-price {
          color: rgb(226, 44, 44);
          font-weight: 700;
          padding-left: 0.5rem;
          span {
            font-size: 1.5rem;
            em {
              font-size: 1rem;
            }
          }
        }
      }
      .num-wrap {
        width: calc(100% - 2rem);
        margin: 1rem;
        height: 5rem;
        display: flex;
        justify-content: space-between;
        h3 {
          font-size: 1.5rem;
        }
        div {
          button:first-child {
            width: 2rem;
            height: 2.8rem;
            border-radius: 0.5rem 0 0 0.5rem;
          }
          button:last-child {
            width: 2rem;
            height: 2.8rem;
            border-radius: 0 0.5rem 0.5rem 0;
          }
          .numtext {
            width: 4rem;
            height: 2.8rem;
            text-align: center;
            background-color: #eee;
            margin: 0 0.3rem;
          }
        }
      }
      .oper-wrap {
        width: calc(100% - 2rem);
        height: 4.2rem;
        display: flex;
        margin: 0 1rem;
        justify-content: space-between;
        button {
          width: 16rem;
          height: 4.2rem;
          font-weight: 700;
          color: #fff;
          border-radius: 2rem;
        }
        .addCart {
          background-color: #ffc707;
        }
        .buy {
          width: 16.7rem;
          background-color: #f91f28;
        }
      }
    }
  }
  // 城市选择的弹出框
  .city-select {
    position: relative;
    .mint-popup {
      width: 100%;
      height: 30rem;
      border-radius: 1.5rem 1.5rem 0 0;
      background-color: #fff;
      h3 {
        font-size: 1.6rem;
        text-align: center;
        font-weight: 700;
        margin-top: 1rem;
      }
      .icon-chahao {
        position: absolute;
        right: 2rem;
        top: 2rem;
      }
    }
  }
  .footer {
    position: fixed;
    bottom: 0;
    left: 0;
    display: flex;
    justify-content: space-around;
    width: 100%;
    height: 4.2rem;
    background-color: #fff;
    color: #666;
    border-top: 0.1rem solid rgba(255, 255, 255, 0.1);
    padding: 0.5rem;
    > div {
      text-align: center;
      flex: 1;
      display: flex;
      flex-direction: column;
      i {
        font-size: 2rem;
      }
    }
    button {
      //   flex:1 ----flex-grow: 1 flex-shink：1 flex-basis：0%;
      flex: 1;
      flex-basis: 7rem;
      border-radius: 2rem;
      font-weight: 700;
      width: 12rem;
      height: 4.2rem;
      margin-right: 1rem;
      color: #fff;
    }
    .shop {
      margin-left: 1rem;
    }
    .cart {
      position: relative;
      margin-right: 1rem;
      em {
        border-radius: 40%;
        border: 0.1rem solid red;
        position: absolute;
        right: -0.3rem;
        top: 0rem;
        color: #f91f28;
        font-weight: 700;
      }
    }
    .addCart {
      background-color: #ffc707;
    }
    .buy {
      background-color: #f91f28;
    }
  }
}
</style>
